﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace EMoney.IStock.Client.Framework
{
    public class CookieHelper
    {
        #region 金融平台用
        /// <summary>
        /// 添加Cookies 金融平台用
        /// </summary>
        /// <param name="key"></param>
        /// <param name="values"></param>
        public static void AddCookies(string key, string values)
        {
            HttpCookie cerCookie = new HttpCookie(key, values);
            System.Web.HttpContext.Current.Response.Cookies.Add(cerCookie);
        }

        /// <summary>
        /// 单项Cookies赋值
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="vale">键值</param>
        /// <param name="expires">有效时间</param>
        /// <returns></returns>
        public static void AddCookie(string key, string vale, DateTime expires)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(key);
                Cookie.Expires = expires;
                Cookie.Value = vale;
                HttpContext.Current.Response.Cookies.Add(Cookie);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 获取Cookies 金融平台用
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCookies(string key)
        {
            var cookie = System.Web.HttpContext.Current.Request.Cookies[key];
            if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
            {
                return System.Web.HttpContext.Current.Request.Cookies[key].Value;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 读取单项Cookies 金融平台用
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public static string GetCookie(string key)
        {
            HttpCookie Cookie = HttpContext.Current.Request.Cookies[key];
            if (Cookie != null)
            {
                return Cookie.Value.ToString();
            }
            else
            {
                return null;
            }
        }

        #endregion


        public static void SetCookie(string cookieName, string key, string value)
        {
            HttpCookie cookietwo = new HttpCookie(cookieName);
            cookietwo.Values[key] = value;
            //cookietwo.Expires = DateTime.Now.AddHours(24);
            HttpContext.Current.Response.Cookies.Add(cookietwo);
        }

        public static void SetCookie(string cookieName, string key, string value, int hours)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Values[key] = value;
            cookie.Expires = DateTime.Now.AddHours(hours);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string GetCookie(string cookieName, string key)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                if (HttpContext.Current.Request.Cookies[cookieName][key] != null)
                {
                    return HttpContext.Current.Request.Cookies[cookieName][key].ToString();
                }
            }
            return "";
        }

        public static void RemoveCookie(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                HttpContext.Current.Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(-1); 
            }
        }
    }
}
